package org.firebug.spring.boot.swagger.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.firebug.spring.boot.swagger.web.bean.core.Response;
import org.firebug.spring.boot.swagger.web.bean.ovo.UserOVO;
import org.firebug.spring.boot.swagger.web.bean.voi.UserModifyVOI;
import org.firebug.spring.boot.swagger.web.bean.voi.UserQueryVOI;
import org.firebug.spring.boot.swagger.web.bean.voi.UserSaveVOI;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * http://localhost:8080/spring-boot-api/user/save?version=1
 * 1. segmentation: -
 * 2. uri:   lower case
 * 3. param: lower camel
 * 4. json:  lower camel
 */
@RestController
@RequestMapping(value = "/user")
@Api(tags = {"/user"}, description = "用户")
public class UserController {

    /**
     * Content-Type = application/json; charset=UTF-8
     * RequestMethod = POST
     * http://localhost:8080/spring-boot-api/user/save?version=1
     */
    @ApiOperation(value = "保存1", notes = "保存2")
    @RequestMapping(value = "/save", method = RequestMethod.POST, params = {"version=1"})
    public Response<Object> save(@Validated @RequestBody UserSaveVOI voi, Errors errors) {
        if (errors.hasErrors()) {
            return Response.getInstance(400, "bad request", null);
        }
        // TODO
        return Response.getInstance(200, "OK", null);
    }

    /**
     * Content-Type = application/json; charset=UTF-8
     * RequestMethod = DELETE
     * http://localhost:8080/spring-boot-api/user/remove/1,2?version=1
     */
    @ApiOperation(value = "删除1", notes = "删除2")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path", allowMultiple = true)
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE, params = {"version=1"})
    public Response<Object> remove(@PathVariable long... id) {
        // TODO
        return Response.getInstance(200, "OK", null);
    }

    /**
     * Content-Type = application/json; charset=UTF-8
     * RequestMethod = PUT
     * http://localhost:8080/spring-boot-api/user/modify?version=1
     */
    @ApiOperation(value = "修改1", notes = "修改2")
    @RequestMapping(value = "/modify", method = RequestMethod.PUT, params = {"version=1"})
    public Response<Object> modify(@Validated @RequestBody UserModifyVOI voi, Errors errors) {
        if (errors.hasErrors()) {
            return Response.getInstance(400, "bad request", null);
        }
        // TODO
        return Response.getInstance(200, "OK", null);
    }

    /**
     * Content-Type = application/x-www-form-urlencoded
     * RequestMethod = GET
     * http://localhost:8080/spring-boot-api/user/query/1?verson=1
     */
    @ApiOperation(value = "查询1", notes = "查询2")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET, params = {"version=1"})
    public Response<UserOVO> query(@PathVariable long id) {
        // TODO
        return Response.getInstance(200, "OK", new UserOVO());
    }

    /**
     * Content-Type = application/x-www-form-urlencoded
     * RequestMethod = GET
     * http://localhost:8080/spring-boot-api/user/list?name=felix&verson=1
     */
    @ApiOperation(value = "查询1", notes = "查询2")
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET, params = {"version=1"})
    public Response<List<UserOVO>> list(@Validated UserQueryVOI voi, Errors errors) {
        if (errors.hasErrors()) {
            return Response.getInstance(400, "bad request", null);
        }
        // TODO
        return Response.getInstance(200, "OK", Arrays.asList(new UserOVO()));
    }
}

